package product;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class ProductInput extends JInternalFrame {
	Map<String, ProductData> hm = new HashMap<String, ProductData>();
	boolean mdf;
	
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JTextField pcInput;
	private JTextField pnInput;
	private JTextField cosInput;
	private JTextField eaInput;
	private JTextField totInput;
	private JButton btnSearch;
	private JButton btnSave;
	private JButton btnClr;
	
	public ProductInput() {
		super("ProductInput",true,true,false,false);		
		
		setBounds(100, 100, 335, 173);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getLabel_3());
		getContentPane().add(getPcInput());
		getContentPane().add(getPnInput());
		getContentPane().add(getCosInput());
		getContentPane().add(getEaInput());
		getContentPane().add(getTotInput());
		getContentPane().add(getBtnSearch());
		getContentPane().add(getBtnSave());
		getContentPane().add(getBtnClr());

	}
	public void setD(Map<String, ProductData> map, boolean a){
		this.hm = map;
		this.mdf=a;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("제품코드");
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("제품명");
			label.setHorizontalAlignment(SwingConstants.RIGHT);
			label.setBounds(12, 35, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("단가");
			label_1.setHorizontalAlignment(SwingConstants.RIGHT);
			label_1.setBounds(12, 60, 57, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("수량");
			label_2.setHorizontalAlignment(SwingConstants.RIGHT);
			label_2.setBounds(12, 85, 57, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("금액");
			label_3.setHorizontalAlignment(SwingConstants.RIGHT);
			label_3.setBounds(12, 110, 57, 15);
		}
		return label_3;
	}
	private JTextField getPcInput() {
		if (pcInput == null) {
			pcInput = new JTextField();
			pcInput.setBounds(81, 7, 116, 21);
			pcInput.setColumns(10);
		}
		return pcInput;
	}
	private JTextField getPnInput() {
		if (pnInput == null) {
			pnInput = new JTextField();
			pnInput.setEditable(false);
			pnInput.setColumns(10);
			pnInput.setBounds(81, 32, 116, 21);
		}
		return pnInput;
	}
	private JTextField getCosInput() {
		if (cosInput == null) {
			cosInput = new JTextField();
			cosInput.setEditable(false);
			cosInput.setColumns(10);
			cosInput.setBounds(81, 57, 116, 21);
		}
		return cosInput;
	}
	private JTextField getEaInput() {
		if (eaInput == null) {
			eaInput = new JTextField();
			eaInput.setColumns(10);
			eaInput.setBounds(81, 82, 116, 21);
		}
		return eaInput;
	}
	private JTextField getTotInput() {
		if (totInput == null) {
			totInput = new JTextField();
			totInput.setHorizontalAlignment(SwingConstants.RIGHT);
			totInput.setEditable(false);
			totInput.setColumns(10);
			totInput.setBounds(81, 107, 116, 21);
		}
		return totInput;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("조회");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try{
						if(pcInput.getText().trim().equals("")){ // 조회 칸이 공백일때,
							JOptionPane.showMessageDialog(null, "상품 코드를 제대로 입력해주세요."); 
							return;
						}
						ProductData pd = hm.get(pcInput.getText());
						pcInput.setText(hm.get(pcInput.getText()).pCode); // 각 Textfield 를 채워넣음.
						pnInput.setText(hm.get(pcInput.getText()).pName);
						cosInput.setText(String.valueOf(hm.get(pcInput.getText()).cost));
						if(hm.get(pcInput.getText()).ea != 0 && hm.get(pcInput.getText()).tot != 0){
							eaInput.setText(String.valueOf(hm.get(pcInput.getText()).ea));
							totInput.setText(String.valueOf(pd.decimalFormat()) + " 원");
						}
						pcInput.setEditable(false); // 조회 뒤 수량만 조정할 수 있도록 코드 입력칸 수정불가
					}catch(NullPointerException npe){
						JOptionPane.showMessageDialog(null, "입력하신 코드는 존재하지 않는 데이터입니다. 올바른 코드를 입력해주세요.");
					}			
					
				}
			});
			btnSearch.setBounds(209, 6, 97, 47);
		}
		return btnSearch;
	}
	private JButton getBtnSave() {
		if (btnSave == null) {
			btnSave = new JButton("저장");
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					try{
						if(eaInput.getText().trim().equals("")){
							JOptionPane.showMessageDialog(null, "빈칸을 모두 채워주세요");
							return;
						}else{
							Date temp = hm.get(pcInput.getText()).nal; // 생산 날짜 수정 방지를 위한 temp 
							
							ProductData pd = new ProductData(pcInput.getText(), pnInput.getText(),
									Integer.parseInt(eaInput.getText()),Integer.parseInt(cosInput.getText()),temp); // 수량을 더해서 ProductData에 저장.
							hm.put(pcInput.getText(), pd); // 저장된 ProductData를 HashMap 에 저장.			
										
							totInput.setText(pd.decimalFormat()+" 원"); // 입력받은 수량과 단가를 곱한 값을 금액에 출력
							pcInput.requestFocus();
							JOptionPane.showMessageDialog(null, "데이터가 저장되었습니다");
							mdf=true; // 수정여부
						}
					}catch(NumberFormatException nfe){
						JOptionPane.showMessageDialog(null, "단가는 숫자만 입력해주세요."); // 숫자외 문자 입력을 시도했을때 메세지 출력.
					}
					
				}
			});
			btnSave.setBounds(209, 56, 97, 47);
		}
		return btnSave;
	}
	private JButton getBtnClr() {
		if (btnClr == null) {
			btnClr = new JButton("Clear");
			btnClr.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					pcInput.setText("");
					pnInput.setText("");
					cosInput.setText("");
					eaInput.setText("");
					totInput.setText("");
					pcInput.setEditable(true); // 코드 입력칸 활성화
					
				}
			});
			btnClr.setBounds(209, 106, 97, 23);
		}
		return btnClr;
	}
}
